/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package player.util;

import java.io.File;
import org.jaudiotagger.audio.AudioFile;
import org.jaudiotagger.audio.AudioFileIO;
import org.jaudiotagger.tag.FieldKey;
import org.jaudiotagger.tag.Tag;
import player.model.Track;

/**
 *
 * @author Andrej
 */
public class TrackLoader
{
    public static Track loadTrack(File file) throws Exception
    {
        Track track = new Track();
        track.setFilename(file.getAbsolutePath());

        AudioFile audioFile = AudioFileIO.read(file);

        track.setLenght(audioFile.getAudioHeader().getTrackLength());
        track.setBitrate(audioFile.getAudioHeader().getBitRateAsNumber());
        Tag tag = audioFile.getTag();

        track.setAlbum(tag.getFirst(FieldKey.ALBUM));
        track.setArtist(tag.getFirst(FieldKey.ARTIST));
        track.setGenre(tag.getFirst(FieldKey.GENRE));
        track.setTrackName(tag.getFirst(FieldKey.TITLE));


        return track;
    }
}
